package com.jiuan.retrofit.util

import android.text.TextUtils
import java.io.File
import java.io.IOException
import java.io.Closeable

class IOUtils {

    companion object {
        fun closeQuietly(closeable: Closeable?) {
            if (closeable != null)
                try {
                    closeable!!.close()
                } catch (e: Exception) {
                    Logger.w(e)
                }

        }

        /**
         * Create a folder, If the folder exists is not created.

         * @param folderPath folder path.
         * *
         * @return True: success, or false: failure.
         */
        fun createFolder(folderPath: String): Boolean {
            if (!TextUtils.isEmpty(folderPath)) {
                val folder = File(folderPath)
                return createFolder(folder)
            }
            return false
        }

        /**
         * Create a folder, If the folder exists is not created.

         * @param targetFolder folder path.
         * *
         * @return True: success, or false: failure.
         */
        fun createFolder(targetFolder: File): Boolean {
            if (targetFolder.exists()) {
                if (targetFolder.isDirectory)
                    return true
                targetFolder.delete()
            }
            return targetFolder.mkdirs()
        }

        /**
         * Create a new file, if the file exists, delete and create again.

         * @param filePath file path.
         * *
         * @return True: success, or false: failure.
         */
        fun createNewFile(filePath: String): Boolean {
            if (!TextUtils.isEmpty(filePath)) {
                val file = File(filePath)
                return createNewFile(file)
            }
            return false
        }

        /**
         * Create a new file, if the file exists, delete and create again.

         * @param targetFile file.
         * *
         * @return True: success, or false: failure.
         */
        fun createNewFile(targetFile: File): Boolean {
            if (targetFile.exists())
                delFileOrFolder(targetFile)
            try {
                return targetFile.createNewFile()
            } catch (e: IOException) {
                return false
            }

        }

        /**
         * Delete file or folder.

         * @param path path.
         * *
         * @return is succeed.
         * *
         * @see .delFileOrFolder
         */
        fun delFileOrFolder(path: String): Boolean {
            return delFileOrFolder(File(path))
        }

        /**
         * Delete file or folder.

         * @param file file.
         * *
         * @return is succeed.
         * *
         * @see .delFileOrFolder
         */
        fun delFileOrFolder(file: File?): Boolean {
            if (file == null || !file.exists()) {
                // do nothing
            } else if (file.isFile)
                file.delete()
            else if (file.isDirectory) {
                val files = file.listFiles()
                if (files != null)
                    for (sonFile in files)
                        delFileOrFolder(sonFile)
                file.delete()
            }
            return true
        }
    }
}
